package pl.smath.regex;

import junit.framework.TestCase;
import pl.smath.expression.parser.ExpressionParserException;
import pl.smath.regex.exceptions.ParseError;
import pl.smath.regex.tree.Parser;
import pl.smath.regex.tree.vertexes.Vertex;

public class PatternSerializationTestCase extends TestCase {

	 /** 
     * Tests valid serialization of expressions.
     */
	
    private static final String[][] TEST_DATA = {
        	{"Number(a) + Variable(x)", "PLUS[NUMBER['A'],VARIABLE['X']]"},
        	{"Number(a)*Variable(y)=2", "RELATION['=',PRODUCT[NUMBER['A'],VARIABLE['Y']],CONST[2]]"},
        	{"Number(a)*Variable(y)[=2]", "PRODUCT[NUMBER['A'],VARIABLE['Y']],CONST[2];RELATION['=',PRODUCT[NUMBER['A'],VARIABLE['Y']],CONST[2]]"},
        	{"Number(a)[+|-]1=2", "RELATION['=',PLUS[NUMBER['A'],CONST[1]],CONST[2]];RELATION['=',MINUS[NUMBER['A'],CONST[1]],CONST[2]]"},
        	{"Sum{n=0, n=infinity}{Number(a_n)*Variable(x)^n}", "SUM['N','0','INF',PRODUCT[NUMBER[A], EXP[VARIABLE[X],SUM_ITERATOR]]]"},
    };
    
    
    public void testData()  throws ExpressionParserException{
    	Parser parser = new Parser();
    	Vertex vertex;
        
        for(int i = 0; i < TEST_DATA.length; i++){
        		try {
        			vertex = parser.parse( TEST_DATA[i][0] );
        			assertEquals(TEST_DATA[i][1], vertex.serialize());
        		}
        		catch (ParseError error) {
        			assertFalse("Parse Error" + error.getMessage(), false);
        		}
        }
    }
}
